package cn.java.controller;

import cn.java.bean.Student;
import cn.java.mapper.StudentMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: Yz
 * description:
 * date: 2020/11/25
 */

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentMapper studentMapper;

    @RequestMapping("/login")
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //利用request对象获取URL请求传来的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", username);
        paramMap.put("ID", password);
        List<Student> studentList = studentMapper.selectByMap(paramMap);

        // 查找到了说明登录成功
        if (studentList.size() >= 1) {
            request.getRequestDispatcher("/pages/success.jsp").forward(request, response);
        } else {
            // 密码或者账号不正确
            response.sendRedirect("/pages/fail.jsp");
        }

    }

}
